package com.gametime.dao.spring;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.gametime.dao.TeamDAO;
import com.gametime.model.Team;

public class SpringTeamDAOImpl implements TeamDAO
{

  private JdbcTemplate jdbcTemplate;
  
  private static final class AllTeamsRowMapper implements RowMapper<Team> {

    @Override
    public Team mapRow(ResultSet rs, int arg1) 
    throws SQLException
    {
      // TODO Auto-generated method stub
      return new Team
          (
              rs.getInt("team_id"),
              rs.getString("club"),
              rs.getString("locale"), 
              null
          );
    }      
}
  
  public void setDataSource(DataSource dataSource) 
  {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
  }
  
  public ArrayList<Team> getAllTeams( )throws Exception {
    final String sql="select team_id, locale, club from team where active_flag = 'Y'";
    return (ArrayList<Team>) this.jdbcTemplate.query(sql, new AllTeamsRowMapper());
  }
}
